package component.action;

import javax.annotation.Resource;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import component.Action;
import component.HttpClient;
import dto.AdSpaceToMDto;
import dto.ResponseDto;
import dto.StatusDto;
import handler.MerchantWebSocketHandler;
import pojo.Advertisement;
import service.AdvertisementService;
import util.JsonUtil;


@Configuration
@PropertySource("classpath:default.properties")
@Component("AdvertiseStatusAction")
public class AdvertiseStatusAction implements Action {
	
	
	
	@Resource
	private AdvertisementService advertiseService;
	@Resource(name="merchantWebSocketHandler")
	private MerchantWebSocketHandler handler;
	
	 @Resource
	 Environment env;
	 @Resource(name="jerseyClient")
	 HttpClient httpClient;
	 
	 //切面
	@Override
	public void action(Object object) {
		AdSpaceToMDto adSpaceToMDto = (AdSpaceToMDto) object;
		//存储数据库，提醒在线用户
		Advertisement advertisement = advertiseService.findById(adSpaceToMDto.getAdsId());
		if (advertisement!=null) {
			advertisement.setStatus(adSpaceToMDto.getStatus());
			advertiseService.update(adSpaceToMDto.getAdsId(),adSpaceToMDto.getStatus(),advertisement.getMerchant().getId());
			System.out.println(env.getProperty("get.status.url")+advertisement.getMerchant().getName());
			String status =httpClient.get(env.getProperty("get.status.url")+advertisement.getMerchant().getName());
	        StatusDto statusDto = (StatusDto) JsonUtil.toJsonObject(status, StatusDto.class);
	        if (StatusDto.WHITE_LIST.equals(statusDto.getStatus())) {
	        	ResponseDto responseDto = new ResponseDto(3,null,adSpaceToMDto.getAdsId()+" is "+adSpaceToMDto.getStatus());
				handler.sendTo(advertisement.getMerchant().getId(), JsonUtil.toJsonString(responseDto));
			}
		}
		
	}

}
